﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Drawing;

public class QuBlack : MonoBehaviour {

    public UILabel INFilePath;
    public UILabel InnewFilePath;
    public UILabel INtianjiaString;
    public UILabel log;

    private Bitmap srcBitmap;
    private Bitmap newSrcBitmap;
    //public string FilePath;
    //public string newFilePath;
    //public string tianjiaString;

    void Awake()
    {
        //log = this.GetComponent<UILabel>();
    }
    

    public void quImageRGB()
    {
        string FilePath = INFilePath.text;
        string newFilePath = InnewFilePath.text;
        //string tianjiaString = INtianjiaString.text;

        string[] files = Directory.GetFiles( FilePath );
     
        DirectoryInfo folder = new DirectoryInfo( FilePath );
        FileInfo[] infos = folder.GetFiles( "*.PNG" );
        int len = infos.Length;
       // for( int i = 0;  i < len; i++ )
       for(int i=len-1;i>=0;i-- )
        {
            //srcBitmap = (Bitmap)System.Drawing.Image.FromFile( infos[i].Name, false );
            //Bitmap bit = new Bitmap( @"C:\Users\Administrator\Desktop\zfd\chuanqiRes\38690.PNG" );
            
            string newpath = newFilePath + @"\" +  infos[i].Name;
            
            string path = files[i];
            Bitmap bit = new Bitmap(path);
            newSrcBitmap = RGB2Gray( bit );
            newSrcBitmap.Save( newpath, System.Drawing.Imaging.ImageFormat.Png );
            //Debug.Log( "1111" );

        }
    }


    public static Bitmap RGB2Gray( Bitmap srcBitmap )
    {
        System.Drawing.Color srcColor;
        int wide = srcBitmap.Width;
        int height = srcBitmap.Height;
        bool exc = false;
        if( wide < height )
        {
            int exchange;
            exchange = height;
            height = wide;
            wide = exchange;
            exc = true;
        }
        //Debug.Log( height + "___________" + wide );
        //for( int y = 0; y < height; y++ )
        for(int y=height-1;y>=0;y-- )
           // for( int x = 0; x < wide; x++ )
           for(int x=wide-1;x>=0;x-- )
            {
                
                if( exc == false )
                {
                    //获取像素的ＲＧＢ颜色值
                    srcColor = srcBitmap.GetPixel( x, y );
                    if( srcColor.Name == "ff000000" )
                        //设置像素点的RGB值
                        srcBitmap.SetPixel( x, y, System.Drawing.Color.FromArgb( 0, 255, 255, 255 ) );
                    else
                        continue;
                }
                else if( exc == true )
                {
                    //获取像素的ＲＧＢ颜色值
                    srcColor = srcBitmap.GetPixel( y, x );
                    if( srcColor.Name == "ff000000" )
                        //设置像素点的RGB值
                        srcBitmap.SetPixel( y, x, System.Drawing.Color.FromArgb( 0, 255, 255, 255 ) );
                    else
                        continue;
                }
                else
                {
                    continue;
                }                  

            }

        return srcBitmap;

    }


    public void Exit()
    {
        Application.Quit();
    }

    //void Start () {
        //File.
       //string[] files=Directory.GetFiles(FilePath);

       // DirectoryInfo folder = new DirectoryInfo(FilePath);
       // FileInfo[] infos = folder.GetFiles("*.jpg");
       //// for (int i = 0; i <= files.Length; i++)
       //// {
       ////     FileStream file = File.Create(files[i]);

       // //     File.
       // // }

       // //第二种方法 

       // for (int i=0;i<infos.Length;i++)
       // {
            
       //     int info = (int)Mathf.Sqrt(infos.Length);

       //     //string oldname =  files[i].Replace(@"\\",@"\");

       //     //string newname = newFilePath + @"\" + tianjiaString + infos[i].Name;
       //     string newname = newFilePath + @"\" + tianjiaString + i / info + "_" + i % info+".jpg";
       //     //File.Move(oldname, newname);
       //     File.Move(files[i], newname);
       //     //file.MoveTo(FilePath + @"\\" + newname);
       // }

  //  }

    // Update is called once per frame
    void Update () {
	
	}
    //public static FileStream Open(string path, FileMode mode)

}
